package com.sc.test.recyclerview2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sc.test.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Shen Chao on 2018-03-12-012 下午 21:24.
 */

public class SwipeToDismissAdapter extends RecyclerView.Adapter<SwipeToDismissAdapter.Viewholder> {
    private ArrayList<String> mData;

    public SwipeToDismissAdapter(ArrayList<String> mData) {
        this.mData = mData;
    }

    @Override
    public Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        //实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.view_rv_item, parent, false);
        //实例化viewholder
        Viewholder holder = new Viewholder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final Viewholder holder, int position) {
        //绑定数据
        holder.tv_item.setText(mData.get(position));
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public void delete(int position) {
        if (position < 0 || position > mData.size()) {
            return;
        }
        mData.remove(position);
        notifyItemRemoved(position);
    }

    public static class Viewholder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_tv)
        TextView tv_item;

        public Viewholder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
